package cn.edu.zsc.rbac.view;

import cn.edu.zsc.rbac.domain.vo.RoleVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class RoleView {
    private RoleVo[] cache;
    private static RoleView singleton;
    private Scanner input = new Scanner(System.in);
    private List<RoleVo> roleList = new ArrayList<>();

    private RoleView() {
        // 初始化测试角色（实际可从数据库/配置加载 ）
        roleList.add(new RoleVo(1, "普通员工"));
        roleList.add(new RoleVo(2, "管理员"));
    }

    public static RoleView getInstance() {
        if (singleton == null) {
            singleton = new RoleView();
        }
        return singleton;
    }

    public void list() {
        cache = roleList.toArray(new RoleVo[0]);
        System.out.println("角色ID\t角色名称");
        for (RoleVo role : cache) {
            System.out.printf("%d\t%s\n", role.getId(), role.getRoleName());
        }
    }

    // 对外提供 cache 访问（解决原报错的 private 权限问题 ）
    public RoleVo[] getCache() {
        return cache;
    }

    public RoleVo getCacheBy() {
        System.out.print("请输入角色ID：");
        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("输入非法，角色ID必须为整数！");
            return null;
        }
        int roleId = input.nextInt();
        input.nextLine();

        if (cache != null) {
            for (RoleVo role : cache) {
                if (role.getId() == roleId) {
                    return role;
                }
            }
        }
        System.out.println("未找到对应ID的角色！");
        return null;
    }

    public void show() {
    }

    public RoleVo getCacheById() {
        return null;
    }
}